//
//  MSRegistViewController.m
//  SocietySale
//
//  Created by Mike on 16/11/1.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSRegistViewController.h"
#import "MSRegistSecondViewController.h"

@interface MSRegistViewController ()
@property (nonatomic, strong) GXButton *nextBtn;
@property (nonatomic, strong) UITextField   *phoneTextField;
@end

@implementation MSRegistViewController

- (void)initNavigationBar{
    [super initNavigationBar];
    leftBarBtn.hidden = NO;
    if (self.registType == MSRegistType_Regist) {
        titleLable.text = @"输入手机号";
    }
    else if (self.registType == MSRegistType_ForgetPassword){
        titleLable.text = @"找回密码";
    }
    else if (self.registType == MSRegistType_ResetPassword){
        titleLable.text = @"修改密码";
    }
}

- (void)initUI{
    self.contentView.backgroundColor = kDefaultViewBackgroundColor;
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 25, SCREEN_WIDTH, 50)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:bgView];
    
    DOImageView *icon = [[DOImageView alloc] initWithFrame:CGRectMake(22, 15.5, 19, 19)];
    icon.imgURLStr = @"mobile";
    [bgView addSubview:icon];
    
    self.phoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(51, 0, SCREEN_WIDTH - 63 ,50)];
    self.phoneTextField.textColor = [UIColor blackColor];
    self.phoneTextField.font = HeitiFont(14);
    self.phoneTextField.placeholder = @"请填写您的手机号码";
    self.phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [bgView addSubview:self.phoneTextField];
    
    [self.phoneTextField addTarget:self action:@selector(customTextFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    
    self.nextBtn = [[GXButton alloc] initWithFrame:CGRectMake(10, bgView.bottom +25, SCREEN_WIDTH-20, 45)];
    self.nextBtn.titleLabel.font = HeitiFont(14);
    [self.nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [self.nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.nextBtn.backgroundColor = kBlueColor;
    self.nextBtn.layer.cornerRadius = 2;
    [self.nextBtn addTarget:self action:@selector(nextClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.nextBtn];
    
    if (self.registType == MSRegistType_Regist) {
        UILabel *protocolLabel = [[UILabel alloc] initWithFrame:CGRectMake(32, self.nextBtn.bottom+10, 120, 25)];
        protocolLabel.text = @"点击下一步代表同意";
        protocolLabel.font = HeitiFont(12);
        protocolLabel.textColor = kLightGrayColor;
        [self.contentView addSubview:protocolLabel];
        
        HyperlinksButton *clauseBtn = [[HyperlinksButton alloc] initWithFrame:CGRectMake(135, self.nextBtn.bottom+10, 150, 25)];
        [clauseBtn setColor:kCyanColor];
        clauseBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        //    clauseBtn.backgroundColor = kDarkGrayColor;
        [clauseBtn setTitle:@"《销傲江湖服务使用协议》" forState:UIControlStateNormal];
        [clauseBtn setTitleColor:kCyanColor forState:UIControlStateNormal];
        [clauseBtn addTarget:self action:@selector(protocolClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:clauseBtn];
    }
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];
    [self.contentView addGestureRecognizer:tap];
}

- (void)testDataFunc{
    
}

- (void)updateViewController:(NSNotification *)note {
    NSDictionary *responseDic = note.userInfo;
    if (responseDic[Receiver]==self.VctrID) {
        if (responseDic[@"VerifyMobile"]!=nil){//注册验证码
            
        }
        if (responseDic[@"ForgetPassword"]!=nil){//密码证码
            
        }
        [HDUtil showMessage:@"短信验证码已发送"];
        MSRegistSecondViewController *vc = [[MSRegistSecondViewController alloc] init];
        vc.registType = self.registType;
        vc.phoneNum = self.phoneTextField.text;
        [rootNavigation pushViewController:vc animated:YES];
    }
}

- (void)nextClick{
    if ([[self.phoneTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0) {
        [HDUtil showMessage:@"请输入手机号码"];
        return;
    }
    if (self.phoneTextField.text.length && ![HDUtil checkMoblieNumber:self.phoneTextField.text]) {
        [HDUtil showMessage:@"请输入正确的手机号码"];
        return;
    }
    if (self.registType == MSRegistType_Regist){
        [netEngine requestVerifyMobileWithTarget:self.VctrID hudView:self.contentView mobile:self.phoneTextField.text];
    }
    else{
        [netEngine requestForgetPasswordWithTarget:self.VctrID hudView:self.contentView mobile:self.phoneTextField.text];
    }
    //发送获取验证码
//    MSRegistSecondViewController *vc = [[MSRegistSecondViewController alloc] init];
//    vc.registType = self.registType;
//    vc.phoneNum = @"";
//    [rootNavigation pushViewController:vc animated:YES];
}

- (void)protocolClick{
    
}

#pragma mark - UITextFieldEditingChanged
- (void)customTextFieldEditingChanged:(UITextField *)textField{
    NSInteger phoneNumLength = (textField.text.length < kPhoneNumberLength) ? textField.text.length : kPhoneNumberLength;
    textField.text = [textField.text substringWithRange:NSMakeRange(0, phoneNumLength)];
}

- (void)tapClick{
    [self.view endEditing:YES];
}

@end
